package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.TabDataMenuMapper;
import com.powergisol.gis.dao.TabUserMapper;
import com.powergisol.gis.entity.TabDataMenu;
import com.powergisol.gis.entity.TabUser;
import com.powergisol.gis.service.TabDataMenuService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabDataMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: 2019/7/9  14:58
 * Description:
 */
@Service
public class TabDataMenuServiceImpl implements TabDataMenuService {

    @Autowired
    private TabDataMenuMapper tabDataMenuMapper;
    @Autowired
    private TabUserMapper tabUserMapper;


    @Override
    public List<TabDataMenuVo> getTabDataMenuByPId(TabDataMenuVo vo) {
        List<TabDataMenuVo> list = tabDataMenuMapper.getTabDataMenuByPId(vo);
        for (TabDataMenuVo tabDataMenuVo:list) {
            List children = new ArrayList();
            Integer serviceNum = tabDataMenuMapper.countServiceNum(tabDataMenuVo.getUserId(),tabDataMenuVo.getLevelCode());
            tabDataMenuVo.setServiceNum(serviceNum);
            tabDataMenuVo.setChildren(children);
        }
        return list;
    }

    @Override
    public Result saveTabDataMenu(TabDataMenuVo vo) {
        Result result = new Result();
        TabDataMenu menu = tabDataMenuMapper.selectByPrimaryKey(vo.getpId());
        if(null == menu){
            return result.failure(-1,"父级类型不存在");
        }
        if(0 == menu.getIsStatic()){
            return result.failure(-1,"父级类型不可编辑");
        }
        if(!menu.getIsSub()){
            return result.failure(-1,"父级类型不存在下一级");
        }
        Integer num = tabDataMenuMapper.getTabDataNumByName(vo.getDataName());
        if(num > 0){
            return result.failure(-1,"数据类型名称已存在");
        }
        String maxLevel = tabDataMenuMapper.getMaxLevel(menu.getLevelCode());
        if(menu.getLevelCode().equals(maxLevel)){
            maxLevel = maxLevel+"01";
        }else{
            Long maxNum = Long.valueOf(maxLevel)+1;
            maxLevel = maxNum.toString();
        }
        vo.setState(Short.valueOf("1"));
        vo.setLevelCode(maxLevel);
        vo.setIsStatic(Short.valueOf("1"));
        tabDataMenuMapper.insertSelective(vo);
        if(null == vo.getUserId()){
            TabUser tab = tabUserMapper.getByUserName(vo.getUserName());
            vo.setUserId(tab.getId());
        }
        tabDataMenuMapper.insertUserMenu(vo.getUserId(),vo.getId());
        result.setData(vo.getId());
        return result.success("操作成功");
    }

    @Override
    public Result deleteTabDataMenu(Integer id) {
        Result result = new Result();
        if(null == id){
            return result.failure(-1,"请选择一条进行操作");
        }
        TabDataMenu tabDataMenu = tabDataMenuMapper.selectByPrimaryKey(id);
        if(null != tabDataMenu && 0 == tabDataMenu.getIsStatic()){
            return result.failure(-1,"静态菜单不能进行删除");
        }

        TabDataMenuVo vo = new TabDataMenuVo();
        vo.setId(id);
        List<TabDataMenuVo> list = tabDataMenuMapper.getTabDataMenuByPId(vo);
        if(list.size()>0){
            return result.failure(-1,"存在子级类型不能进行删除");
        }
        tabDataMenu.setState(Short.valueOf("0"));
        tabDataMenuMapper.updateByPrimaryKeySelective(tabDataMenu);
        tabDataMenuMapper.deleteUserMenu(id);
        result.setData(tabDataMenu.getpId());
        return result.success("操作成功");
    }
}
